﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Store
{
    public partial class 入库判定结果 : System.Web.UI.Page
    {
        private string sql = @"select [产品入库单].[ID]
,[产品].[品名]+'('+[产品].[钢级]+')'+[产品].[规格] as [产品类别]
,case
when 生产线 between 100 and  199 then '一分厂'
when 生产线 =201 then '二分厂1号线'
when 生产线 =202 then '二分厂2号线'
when 生产线 =501 then '五分厂1号线'
when 生产线 =502 then '五分厂2号线'
end
as 生产单位
,[原料供货单位].[名称] as [原料供货单位]
,[产品入库单].[序号] as [入库序号]
,[产品入库单].炉号 
,[产品入库单].[总重量]
,[产品入库单].[总长度]
,[产品入库单].[总件数]
,[产品入库单].[日期]
,[产品入库单].[检定日期]
,[产品入库单].[是否合格]
from [产品入库单],[产品],[原料供货单位]
where [产品].[ID]=[产品入库单].[产品类别ID] and [原料供货单位].[ID]=[产品入库单].[供货单位ID]
and [产品入库单].[是否合格]=1";
        string isreedonly = "";
        string ishege = "true";
        protected void Page_Load(object sender, EventArgs e)
        {

            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                shengchanxian.Visible = false;
                isreedonly = Request.QueryString["isreadonly"];
                //判断是否合格
                if (ishege == "true")
                {
                    sql = sql +  " and [产品入库单].[是否合格]='" + ishege + "'";
                }
                else if (ishege == "false")
                {
                    sql = sql +  " and [产品入库单].[是否合格]='" + ishege + "'";
                }
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            else {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                       
            }

            
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        //查询按钮
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            if (this.FenChang.Text == "1")
            {
                sql += "and [产品入库单].[生产线] between 100 and 199";
            }
            if (this.FenChang.Text == "2")
            {
                //如果选择了2分厂且有生产线定义
                //如果为全部
                if (DropDownList1.Text == "0")
                {
                    sql += "and [产品入库单].[生产线] between 200 and 299";
                }
                //如果为一号线
                if (DropDownList1.Text == "1")
                {
                    sql += "and [产品入库单].[生产线]=201";
                }
                //如果为二号线
                if (DropDownList1.Text == "2")
                {
                    sql += "and [产品入库单].[生产线]=202";
                }
            }

            if (this.FenChang.Text == "5")
            {
                //如果选择了5分厂且有生产线定义
                    //如果为全部
                    if (DropDownList1.Text == "0")
                    {
                        sql += "and [产品入库单].[生产线] between 500 and 599";
                    }
                    //如果为一号线
                    if (DropDownList1.Text == "1")
                    {
                        sql += "and [产品入库单].[生产线]=501";
                    }
                    //如果为二号线
                    if (DropDownList1.Text == "2")
                    {
                        sql += "and [产品入库单].[生产线]=502";
                    }
                
            }


            
            //如果选择了供货单位
            if (this.GongHuo.Text != "0")
            {
                sql += " and [产品入库单].[供货单位ID]=" + this.GongHuo.Text.Replace("'", "");
            }

            //如果填入了入库序号
            if (this.XuHao.Text != "")
            {
                sql += " and [产品入库单].[序号] = " + this.XuHao.Text.Replace("'", "");
            }
            //如果选择了开始和截止日期
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and [产品入库单].[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and [产品入库单].[日期]<='" + Dt.ToShortDateString() + "'";
            }
            //判断是否合格
            if (ishege == "true")
            {
                sql = sql +  " and [产品入库单].[是否合格]='" + ishege + "'";
            }
            else if (ishege == "false")
            {
                sql = sql +  " and [产品入库单].[是否合格]='" + ishege + "'";
            }
            //产品
            if (ChanPin.SelectedValue != "0")
            {
                sql += " and [产品入库单].[产品类别ID]=" + ChanPin.SelectedValue;
            }
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
        }

        //分厂变化
        protected void FenChang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FenChang.SelectedValue == "5" || FenChang.SelectedValue == "2")
            {
                shengchanxian.Visible = true;
            }
            else {
                shengchanxian.Visible = false;
            }
        }
    }
}
